const initTable = (id, data, formatter)=>{
    $(`#${id}`).bootstrapTable({
        columns: [{
            field: 'permission_name',
            title: '权限名称'
        }, {
            field: 'permission_code',
            title: '权限编码'
        },{
            field: 'created_at',
            title: '创建时间'
        },
            {
                field: 'operate',
                title: '操作',
                align: 'center',
                formatter: formatter
            }],
        data
    })
}

const initPermissionTable = (id, data, formatter)=>{
    $(`#${id}`).bootstrapTable({
        columns: [{
            field: 'name',
            title: '权限名称'
        }, {
            field: 'code',
            title: '权限编码'
        },
            {
                field: 'operate',
                title: '操作',
                align: 'center',
                formatter: formatter
            }],
        data
    })
}

function removePermission(id){
    var data = {
        id
    };
    $.ajax({
        type: 'POST',
        url: '/api/removeRolePermission',
        data: JSON.stringify(data),
        success: function (resp) {
            if (resp.code === 200) {
                alert('删除成功');
                location.reload();
            } else {
                alert(resp.info)
            }
        },
        contentType: 'application/json',
        dataType: 'json'
    });
}

function addPermission(permissionId){
    const searchParams = new URLSearchParams(window.location.search);
    const roleId = searchParams.get('roleId');
    var data = {
        roleId,
        permissionId
    };
    $.ajax({
        type: 'POST',
        url: '/api/addRolePermission',
        data: JSON.stringify(data),
        success: function (resp) {
            if (resp.code === 200) {
                alert('添加成功');
                location.reload();
            } else {
                alert(resp.info)
            }
        },
        contentType: 'application/json',
        dataType: 'json'
    });
}

function getAllPermission(data){
    let current = 1;
    let totalPages=1
    function setPage(page){
        current = page
    }
    function next(){
        if(totalPages>current){
            current++;
        }
    }
    function prev(){
        if(current>1){
            current--;
        }
    }
    function goFirst(){
        current=1;
    }
    function getTotal(){
        return totalPages
    }
    function getCurrent(){
        return current;
    }
    function getData(){
        let data = {
            page: current
        }
        return new Promise((resolve, reject)=>{
            $.ajax({
                type: 'POST',
                url: '/api/getAllPermissions',
                data: JSON.stringify(data),
                success:function(resp){
                    if(resp.code===200){
                        totalPages = resp.data.totalPages
                        resolve(resp.data)
                    } else {
                        reject(resp.info);
                    }
                },
                contentType: 'application/json',
                dataType: 'json'
            })
        })
    }
    return {
        next,
        getData,
        getTotal,
        goFirst,
        setPage,
        getCurrent
    }
}